using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Windows.Forms;

namespace AdatbazisProjekt
{
    public abstract class DalGen
    {
        protected static bool ConnectionCreated;
        protected static bool kapcsolodva;
        protected static SqlConnection mySqlConn; 
	//	protected string strSqlConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\azEnyem.MDF";
    //    protected string strSqlConn = "Data Source=(local); Initial Catalog=azEnyem; User Id=n0r_ka; Integrated Security=true";
        protected SqlDataReader myDataReader;
        protected SqlCommand myComm;
        protected string strSqlConn = "Data Source=SQL_INFO2/SQL_INFO_R2; Initial Catalog=diakinfo34; User Id=diakinfo34; Password=smile34; ";


        protected bool IsConnectCreated()
        {
            return ConnectionCreated;
        }

        protected bool IsConnected()
        {
            return kapcsolodva;
        }

        protected bool MakeConnection()
        {
            // Create the Connection if is was not already created.
            if (ConnectionCreated != true)
            {
                try
                {
                    mySqlConn = new SqlConnection(strSqlConn);
                    mySqlConn.Open();
                    ConnectionCreated = true;
                    mySqlConn.Close();
                    kapcsolodva = false;
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            else { return true; }
        }

        protected bool OpenConnection()
        {
            // Open the Connection when the state is not already open.
            if (kapcsolodva != true)
            {
                try
                {
                    mySqlConn.Open();
                    kapcsolodva = true;
                    return true;
                }
                catch
                {
                    return false;
                }

            }
            else { return true; }
        }

        internal void CloseConnection()
        {
            // Close the Connection when the connection is opened.
            if (kapcsolodva == true)
            {
                mySqlConn.Close();
                kapcsolodva = false;
            }
        }

        protected SqlDataReader mExecuteReader(string sQuery, ref string ErrMess)
        {
            try
            {
                this.OpenConnection();                
                myComm = new SqlCommand(sQuery, mySqlConn);
                myDataReader = myComm.ExecuteReader();
                ErrMess = "OK";
                //			this.CloseConnection();
                return myDataReader;

            }
            catch (SqlException e)
            {
                ErrMess = e.Message;
                this.CloseConnection();
                MessageBox.Show(ErrMess);
                return null;
            }
        }

      
    }
}
